/*
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see <http://www.gnu.org/licenses/>.
 */
package org.evosuite.symbolic.dse.algorithm.explorationalgorithms;

import org.evosuite.symbolic.dse.DSEStatistics;
import org.evosuite.symbolic.dse.DSETestCase;
import org.evosuite.symbolic.dse.algorithm.ExplorationAlgorithm;
import org.evosuite.symbolic.dse.algorithm.strategies.implementations.CachingStrategies.CounterExampleCache;
import org.evosuite.symbolic.dse.algorithm.strategies.implementations.KeepSearchingCriteriaStrategies.TestCasesPendingStrategy;
import org.evosuite.symbolic.dse.algorithm.strategies.implementations.PathExtensionStrategies.ExpandExecutionStrategy;
import org.evosuite.symbolic.dse.algorithm.strategies.implementations.TestCaseBuildingStrategies.DefaultTestCaseBuildingStrategy;
import org.evosuite.symbolic.dse.algorithm.strategies.implementations.TestCaseSelectionStrategies.TopTestCaseSelectionStrategy;
import org.evosuite.testcase.TestCase;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * SAGE's implementation of generational search. It differs in that Evosuite uses incremental branch coverage
 * instead of incremental line coverage to score the test cases.
 *
 * @author Ignacio Lebrero
 */
public class SAGEExplorationAlgorithm extends ExplorationAlgorithm {

    public SAGEExplorationAlgorithm(DSEStatistics statistics, boolean showProgress) {
        super(statistics, showProgress);

        /** Strategies */
        setCachingStrategy(new CounterExampleCache());
        setPathsExpansionStrategy(new ExpandExecutionStrategy());
        setTestCaseBuildingStrategy(new DefaultTestCaseBuildingStrategy());
        setTestCaseSelectionStrategy(new TopTestCaseSelectionStrategy());
        setKeepSearchingCriteriaStrategy(new TestCasesPendingStrategy());
    }

    /**
     * Generational search uses a priority queue for ordering the tests cases by a score which is given by
     * {@link #getTestScore(org.evosuite.testcase.TestCase, boolean)}.
     *
     * @return
     */
    @Override
    protected Queue<DSETestCase> createWorkList() {
        return new PriorityQueue();
    }

    /**
     * Scoring is based on incremental coverage generated by adding a new {@link org.evosuite.testcase.TestCase} to the
     * test Suite.
     * <p>
     * In case the current {@link org.evosuite.testcase.TestCase} diverged, its score is automatically 0.
     *
     * @param newTestCase
     * @param hasPathConditionDiverged
     * @return
     */
    @Override
    protected double getTestScore(TestCase newTestCase, boolean hasPathConditionDiverged) {
        if (hasPathConditionDiverged) {
            statisticsLogger.reportNewTestUnuseful();
            return PATH_DIVERGED_BASED_TEST_CASE_PENALTY_SCORE;
        }

        return getTestCaseAdditionIncrementalCoverage(newTestCase);
    }
}
